﻿namespace EFS.RC.Survey.Controls
{
	public partial class Category : EFS.RC.Survey.Controls.Control
	{
		#region Constructors
		/// Implicit
		public Category()
		{
			InitializeComponent();

			// do not use this one
			ConstructorNotAllowed();
		}

		/// Explicit
		public Category(
			EFS.RC.CC.CH.Entities.Surveys.Category categoryObj, EFS.RC.Survey.Controls.Survey survey)
		{
			// check
			EFS.Common.Verify.Reference(categoryObj, "categoryObj");

			// init
			InitializeComponent();
			//this.Surveys.Survey.AnswerCalculate += calculate;

			// set
			this.CategoryObj = categoryObj;
			this.SurveyObj = survey;


			// register events
			UtilitiesService.SurveyQuestion_GetByCategoryIDCompleted += new System.EventHandler<Utilities.SurveyQuestion_GetByCategoryIDCompletedEventArgs>(UtilitiesService_SurveyQuestion_GetByCategoryIDCompleted);
		}
		#endregion

		#region Properties
		/// Property CategoryObj: Get, Set
		public EFS.RC.CC.CH.Entities.Surveys.Category CategoryObj { get; private set; }
		public EFS.RC.Survey.Controls.Survey SurveyObj { get; private set; }
		#endregion

		#region Utilities


		public int calculate()
		{
			return 0;
		}

		/// Validate
		public System.Boolean Validate()
		{
			// cicle all question controls
			System.Boolean areQuestionsValid = true;
			foreach (EFS.RC.Survey.Controls.Question controlQuestion in stackPanelQuestions.Children)
			{
				if (!controlQuestion.Validate())
				{
					areQuestionsValid = false;
				}
			}

			// ok
			return areQuestionsValid;
		}
		#endregion

		#region LoadData
		/// Load category
		private void LoadCategory()
		{
			// set text
			textBlockCategory.Text = CategoryObj.Name;

			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Loading Questions for Category {0}", CategoryObj.Name);

			// call ws
			UtilitiesService.SurveyQuestion_GetByCategoryIDAsync(CategoryObj.ID.Value);
		}

		/// Load questions
		private void LoadQuestions(EFS.RC.CC.CH.Entities.Surveys.Question[] questions)
		{
			// check
			EFS.Common.Verify.Reference(questions, "questions");

			// make controls
			for (System.Int32 i = 0; i < questions.Length; ++i)
			{
				if (i > 0)
				{
					SleepBetweenRepetitiveAsyncCalls();
				}

				EFS.RC.Survey.Controls.Question controlQuestion = new EFS.RC.Survey.Controls.Question(questions[i], SurveyObj);
				controlQuestion.Name = System.String.Format("controlQuestion_{0}", questions[i].ID.Value);
				controlQuestion.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
				controlQuestion.VerticalAlignment = System.Windows.VerticalAlignment.Top;
				if (i > 0)
				{
					controlQuestion.Margin = new System.Windows.Thickness(0, 5, 0, 0);
				}
				stackPanelQuestions.Children.Add(controlQuestion);
			}
		}
		#endregion

		#region WS Callbacks

		/// SurveyAnswer_GetByQuesionIDCompleted
		private void UtilitiesService_SurveyQuestion_GetByCategoryIDCompleted(object sender, Utilities.SurveyQuestion_GetByCategoryIDCompletedEventArgs e)
		{
			//// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Question[] questions =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Question[]>(e.Result.ToString());

				// show
				LoadQuestions(questions);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve questions.", ex);
			}
		}

		#endregion

		#region Events
		/// Load
		private void ControlCategory_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			LoadCategory();
		}
		#endregion
	}

}